﻿using System;
using BLL;
using DAL;
using Ext.Net;
using System.Data;
using Roles;

namespace ERP.Modules
{
    public partial class ServicesMaintain : System.Web.UI.UserControl
    {
        #region[Define]
        private static bool _flag;
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                CheckPermisstion();
                txtDateBegin.Text = DateTime.Now.ToString();
                GetOrderServicesMaintain();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnExport.Disable(true);
                btnReviewMain.Disable(true);
            }
        }
        //Store
        protected void StoreOrderServicesMaintain_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearch.Text = string.Empty;
            txtDateBeginFillter.Text = string.Empty;
            txtDateEndFillter.Text = string.Empty;
            GetOrderServicesMaintain();
        }
        protected void StoreOrderServicesMaintain_Detail_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            try
            {
                if (_flag)
                {
                    GetOrderServicesMaintainDetailTemp();
                    GetOrderServicesMaintainSum(null, 1, int.Parse(Session["Users_Id"].ToString()));
                }
                else
                {
                    GetOrderServicesMaintainEdit(int.Parse(txtMaintain_ID.Text));
                    GetOrderServicesMaintainSum(int.Parse(txtMaintain_ID.Text), 0, null);
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", "Có lỗi xảy ra trong quá trình tải lại <br/>" + ex.Message);
            }
        }
        protected void StoreOrderWithOrder_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            GetOrder();
            SelectBoxSell.Text = string.Empty;
            grOrderWithOrder.GetStore().RemoveAll();
        }
        protected void StoreServiceReview_Detail_RefreshData(object sender, StoreReadDataEventArgs e)
        {

        }
        //Button
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            _flag = true;
            txtOrderServicesMaintainCode.ReadOnly = true;
            txtDateBegin.ReadOnly = false;
            btnRefreshOrderServicesMaintainCode.Enable(true);
            btnSavePopUp.Enable(true);
            btnAddOrderCode.Enable(true);
            btnDelMaterial.Enable(true);
            ClearText();
            GetAutoID();
            WindowsOrderServicesMaintain.Show();
        }
        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grOrderServicesMaintain.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    _flag = false;
                    txtOrderServicesMaintainCode.ReadOnly = true;
                    btnRefreshOrderServicesMaintainCode.Disable(true);
                    GetOrderServicesMaintainEdit(int.Parse(row.RecordID));
                    GetOrderServicesMaintainSum(int.Parse(row.RecordID), 0, null);
                    WindowsOrderServicesMaintain.Show();
                }
        }
        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            try
            {
                var sm = grOrderServicesMaintain.GetSelectionModel() as CheckboxSelectionModel;
                if (sm != null)
                    foreach (var row in sm.SelectedRows)
                    {
                        var obj = new EtOrderServicesMaintain { Maintain_ID = int.Parse(row.RecordID) };
                        BllOrderServicesMaintain.Delete(obj);
                    }
                GetOrderServicesMaintain();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        protected void btnFillter_Click(object sender, DirectEventArgs e)
        {
            GetOrderServicesMaintainFillterDate();
        }
        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            Insert();
        }
        protected void btnDelMaterial_Click(object sender, DirectEventArgs e)
        {
            if (_flag)
            {
                var sm = grOrderServicesMaintain_Detail.GetSelectionModel() as CheckboxSelectionModel;
                if (sm != null)
                    foreach (var row in sm.SelectedRows)
                    {
                        try
                        {
                            DeleteDetail(int.Parse(row.RecordID), 1);
                        }
                        catch (Exception)
                        {
                            HTML.clsFormat.Alert_ERROR("Thông Báo",
                                "Tồn tại tài liệu con, không xoá được vui lòng xoá tài liệu con trước !");
                        }
                    }
                GetOrderServicesMaintainDetailTemp();
            }
            else
            {
                var sm = grOrderServicesMaintain_Detail.GetSelectionModel() as CheckboxSelectionModel;
                if (sm != null)
                    foreach (var row in sm.SelectedRows)
                    {
                        try
                        {
                            DeleteDetail(int.Parse(row.RecordID), 0);
                        }
                        catch (Exception)
                        {
                            HTML.clsFormat.Alert_ERROR("Thông Báo", "Tồn tại tài liệu con, không xoá được vui lòng xoá tài liệu con trước !");
                        }
                    }
                GetOrderServicesMaintainEdit(int.Parse(txtMaintain_ID.Text));
            }
        }
        protected void btnCancelPopUp_Click(object sender, DirectEventArgs e)
        {
            ClearText();
            WindowsOrderServicesMaintain.Close();
        }
        protected void btnRefreshOrderServicesMaintainCode_Click(object sender, DirectEventArgs e)
        {
            GetAutoID();
        }
        protected void btnAddOrderCode_Click(object sender, DirectEventArgs e)
        {
            var sm = grOrderWithOrder.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null) sm.DeselectAll();
            GetOrder();
            SelectBoxSell.Text = string.Empty;
            grOrderWithOrder.GetStore().RemoveAll();
            WindowOrderWithOrder.Show();
        }
        protected void btnChooseMaterials_Click(object sender, DirectEventArgs e)
        {
            if (_flag)
            {
                InsertDetail();
            }
            else
            {
                UdpateDetail();
            }
        }
        protected void btnReviewMain_Click(object sender, DirectEventArgs e)
        {
            var sm = grOrderServicesMaintain.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    GetOrderServicesMaintainReview(int.Parse(row.RecordID));
                    GetOrderServicesMaintainSumReview(int.Parse(row.RecordID), 0, null);
                    WindowReview.Show();
                }
        }
        protected void btnReview_Click(object sender, DirectEventArgs e)
        {
            Review();
        }
        protected void btnUnReview_Click(object sender, DirectEventArgs e)
        {
            UnReview();
        }
        //Double-Click
        protected void OrderServicesMaintainDblClick(object sender, DirectEventArgs e)
        {
            _flag = false;
            var sm = grOrderServicesMaintain_Detail.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null) sm.DeselectAll();
            int Maintain_ID = int.Parse(e.ExtraParams["Maintain_ID"]);
            txtOrderServicesMaintainCode.ReadOnly = true;
            txtDateBegin.ReadOnly = true;
            btnRefreshOrderServicesMaintainCode.Disable(true);

            GetOrderServicesMaintainEdit(Maintain_ID);
            GetOrderServicesMaintainSum(Maintain_ID, 0, null);
            WindowsOrderServicesMaintain.Show();
        }
        //Cell-Update
        protected void UpdateMaterials_Click(object sender, DirectEventArgs e)
        {
            var GetOrder_Detail = e.ExtraParams["grOrderServicesMaintain_Detail"];
            var dtgrOrder_Detail = JSON.Deserialize<DataTable>(GetOrder_Detail);
            foreach (DataRow rows in dtgrOrder_Detail.Rows)
            {
                if (_flag)
                {
                    if (DateTime.Parse(rows["Maintain_DateEnd"].ToString()) <= DateTime.Parse(txtDateBegin.Text))
                    {
                        HTML.clsFormat.Alert_ERROR("Lỗi", "Ngày bảo hành phải lớn hơn ngày lập phiếu !");
                        GetOrderServicesMaintainDetailTemp();
                        GetOrderServicesMaintainSum(null, 1, int.Parse(Session["Users_Id"].ToString()));
                    }
                    else
                    {
                        UpdateDate(int.Parse(rows["Maintain_Detail_ID"].ToString()),
                               DateTime.Parse(rows["Maintain_DateEnd"].ToString()));
                    }
                }
                else
                {
                    if (CheckPermisstionDobclick())
                    {
                        HTML.clsFormat.Alert_ERROR("Lỗi", "Bạn không được cấp phép thay đổi dữ liệu !");
                    }
                    else
                    {
                        var obj = new EtOrderServicesMaintain { Maintain_ID = int.Parse(txtMaintain_ID.Text) };
                        BllOrderServicesMaintain.GetOrderServicesMaintainCheckReview(obj);
                        if (obj.Msg == 0)
                        {
                            if (DateTime.Parse(rows["Maintain_DateEnd"].ToString()) <= DateTime.Parse(txtDateBegin.Text))
                            {
                                HTML.clsFormat.Alert_ERROR("Lỗi", "Ngày bảo hành phải lớn hơn ngày lập phiếu !");
                                GetOrderServicesMaintainEdit(int.Parse(txtMaintain_ID.Text));
                                GetOrderServicesMaintainSum(int.Parse(txtMaintain_ID.Text), 0, null);
                            }
                            else
                            {
                                UpdateDate(int.Parse(rows["Maintain_Detail_ID"].ToString()),
                                DateTime.Parse(rows["Maintain_DateEnd"].ToString()));
                            }
                        }
                        else
                        {
                            HTML.clsFormat.Alert_ERROR("Lỗi",
                                "Mẫu đơn này đã xét duyệt.Bạn không thể thay đổi mẫu tin !");
                        }  
                    }
                }
            }
        }
        //Search
        protected void Search_Event(object sender, DirectEventArgs e)
        {
            var _clsGetOrder = new BllOrderServicesMaintain();
            var _dtSearch = _clsGetOrder.GetOrderServicesMaintain(txtSearch.Text);
            if (_dtSearch != null && _dtSearch.Rows.Count > 0)
            {
                StoreOrderServicesMaintain.DataSource = _dtSearch;
                StoreOrderServicesMaintain.DataBind();
            }
            else
            {
                grOrderServicesMaintain.GetStore().RemoveAll();
            }
        }
        //Select-box
        protected void SelectBoxSell_Change(object sender, DirectEventArgs e)
        {
            if (_flag)
            {
                GetOrderEdit(int.Parse(SelectBoxSell.SelectedItem.Value));
            }
            else
            {
                if (SelectBoxSell.SelectedItem.Text == txtOrder_Code.Text)
                {
                    GetOrderEdit(int.Parse(SelectBoxSell.SelectedItem.Value));
                }
                else
                {
                    HTML.clsFormat.Alert_ERROR("Lỗi", "Chứng từ đang được sử dụng.Bạn không thể thay đổi chứng từ !");
                    SelectBoxSell.Text = string.Empty;
                }
            }
        }
        #endregion

        #region[Method]
        private void GetOrder()
        {
            var _clsOrder = new BllOrderServicesMaintain();
            var _dtOrder = _clsOrder.GetSell(string.Empty);
            if (_dtOrder != null && _dtOrder.Rows.Count > 0)
            {
                StoreSelectBoxSell.DataSource = _dtOrder;
                StoreSelectBoxSell.DataBind();
            }
            else
            {
                SelectBoxSell.GetStore().RemoveAll();
            }
        }
        private void GetOrderEdit(int id)
        {
            var _clsGetOrderEdit = new BllOrderServicesMaintain();
            var _dtGetOrder = _clsGetOrderEdit.GetSellDetail(string.Empty, id);
            if (_dtGetOrder != null && _dtGetOrder.Rows.Count > 0)
            {
                txtSell_Id.Text = _dtGetOrder.Rows[0]["ID_Sell"].ToString();
                txtOrder_Code.Text = _dtGetOrder.Rows[0]["Sell_Code"].ToString();
                txtCustomerAlias.Text = _dtGetOrder.Rows[0]["Customer_Alias"].ToString();
                txtCustomerName.Text = _dtGetOrder.Rows[0]["Customer_Name"].ToString();
                txtCustomerID.Text = _dtGetOrder.Rows[0]["Customer_ID"].ToString();
                txtCustomerAddress.Text = _dtGetOrder.Rows[0]["Customer_Address"].ToString();
                StoreOrderWithOrder.DataSource = _dtGetOrder;
                StoreOrderWithOrder.DataBind();
            }
            else
            {
                grOrderWithOrder.GetStore().RemoveAll();
            }
        }
        private void GetOrderServicesMaintain()
        {
            var _clsGetOrderServicesMaintain = new BllOrderServicesMaintain();
            var _dtGetOrderServicesMaintain = _clsGetOrderServicesMaintain.GetOrderServicesMaintain(txtSearch.Text);
            if (_dtGetOrderServicesMaintain != null && _dtGetOrderServicesMaintain.Rows.Count > 0)
            {
                StoreOrderServicesMaintain.DataSource = _dtGetOrderServicesMaintain;
                StoreOrderServicesMaintain.DataBind();
            }
            else
            {
                grOrderServicesMaintain.GetStore().RemoveAll();
            }
        }
        private void GetOrderServicesMaintainEdit(int id)
        {
            var _clsGetOrderServicesMaintainEdit = new BllOrderServicesMaintain();
            var _dtGetOrderServicesMaintain = _clsGetOrderServicesMaintainEdit.GetOrderServicesMaintainEdit(id);
            var _dtGetOrderServicesCheck = _clsGetOrderServicesMaintainEdit.GetOrderServicesMaintainEditGrid(id);
            if (_dtGetOrderServicesMaintain != null && _dtGetOrderServicesMaintain.Rows.Count > 0)
            {
                txtMaintain_ID.Text = _dtGetOrderServicesMaintain.Rows[0]["Maintain_ID_"].ToString();
                txtSell_Id.Text = _dtGetOrderServicesMaintain.Rows[0]["ID_Sell"].ToString();
                txtOrderServicesMaintainCode.Text = _dtGetOrderServicesMaintain.Rows[0]["Maintain_Code"].ToString();
                txtOrder_Code.Text = _dtGetOrderServicesMaintain.Rows[0]["Sell_Code"].ToString();
                txtDateBegin.Text = _dtGetOrderServicesMaintain.Rows[0]["Maintain_DateBegin"].ToString();
                txtTaxCode.Text = _dtGetOrderServicesMaintain.Rows[0]["Maintain_TaxCode"].ToString();
                txtDescription.Text = _dtGetOrderServicesMaintain.Rows[0]["Note"].ToString();
                txtCustomerAlias.Text = _dtGetOrderServicesMaintain.Rows[0]["Customer_Alias"].ToString();
                txtCustomerName.Text = _dtGetOrderServicesMaintain.Rows[0]["Customer_Name"].ToString();
                txtCustomerID.Text = _dtGetOrderServicesMaintain.Rows[0]["Customer_ID"].ToString();
                txtCustomerAddress.Text = _dtGetOrderServicesMaintain.Rows[0]["Customer_Address"].ToString();
                var obj = new EtOrderServicesMaintain { Maintain_ID = id };
                BllOrderServicesMaintain.GetOrderServicesMaintainCheckReview(obj);
                if (obj.Msg == 0)
                {
                    btnSavePopUp.Enable(true);
                    btnAddOrderCode.Enable(true);
                    btnDelMaterial.Enable(true);
                }
                else
                {
                    btnSavePopUp.Disable(true);
                    btnAddOrderCode.Disable(true);
                    btnDelMaterial.Disable(true);
                }


                if (_dtGetOrderServicesCheck != null && _dtGetOrderServicesCheck.Rows.Count > 0)
                {
                    StoreOrderServicesMaintain_Detail.DataSource = _dtGetOrderServicesMaintain;
                    StoreOrderServicesMaintain_Detail.DataBind();
                }
                else
                {
                    grOrderServicesMaintain_Detail.GetStore().RemoveAll();
                }
            }
        }
        private void GetOrderServicesMaintainReview(int id)
        {
            var _clsGetOrderServicesMaintainEdit = new BllOrderServicesMaintain();
            var _dtGetOrderServicesMaintain = _clsGetOrderServicesMaintainEdit.GetOrderServicesMaintainEdit(id);
            var _dtGetOrderServicesCheck = _clsGetOrderServicesMaintainEdit.GetOrderServicesMaintainEditGrid(id);
            if (_dtGetOrderServicesMaintain != null && _dtGetOrderServicesMaintain.Rows.Count > 0)
            {
                txtReviewMaintain_ID.Text = _dtGetOrderServicesMaintain.Rows[0]["Maintain_ID_"].ToString();
                txtReviewCode.Text = _dtGetOrderServicesMaintain.Rows[0]["Maintain_Code"].ToString();
                txtReviewSellCode.Text = _dtGetOrderServicesMaintain.Rows[0]["Sell_Code"].ToString();
                txtReviewDateBegin.Text = _dtGetOrderServicesMaintain.Rows[0]["Maintain_DateBegin"].ToString();
                txtReViewTaxCode.Text = _dtGetOrderServicesMaintain.Rows[0]["Maintain_TaxCode"].ToString();
                txtReviewNote.Text = _dtGetOrderServicesMaintain.Rows[0]["Note"].ToString();
                txtReviewCustomerAlias.Text = _dtGetOrderServicesMaintain.Rows[0]["Customer_Alias"].ToString();
                txtReviewCustomerName.Text = _dtGetOrderServicesMaintain.Rows[0]["Customer_Name"].ToString();
                txtReviewAddress.Text = _dtGetOrderServicesMaintain.Rows[0]["Customer_Address"].ToString();
                var obj = new EtOrderServicesMaintain { Maintain_ID = id };
                BllOrderServicesMaintain.GetOrderServicesMaintainCheckReview(obj);
                if (obj.Msg == 0)
                {
                    btnReview.Enable(true);
                    btnUnReview.Disable(true);
                }
                else
                {
                    btnReview.Disable(true);
                    btnUnReview.Enable(true);
                }
                if (_dtGetOrderServicesCheck != null && _dtGetOrderServicesCheck.Rows.Count > 0)
                {
                    StoreServiceReview.DataSource = _dtGetOrderServicesMaintain;
                    StoreServiceReview.DataBind();
                }
                else
                {
                    grServiceReview.GetStore().RemoveAll();
                }
            }
        }
        private void GetOrderServicesMaintainSum(int? id, int flag, int? Users_Id)
        {
            var _clsGetOrderServicesMaintainEdit = new BllOrderServicesMaintain();
            var _dtGetOrderServicesMaintain = _clsGetOrderServicesMaintainEdit.GetSellSum(id, Users_Id, flag);
            if (_dtGetOrderServicesMaintain != null && _dtGetOrderServicesMaintain.Rows.Count > 0)
            {
                lbTotalOver.Text = _dtGetOrderServicesMaintain.Rows[0]["TotalMaterial"].ToString();
            }
        }
        private void GetOrderServicesMaintainSumReview(int? id, int flag, int? Users_Id)
        {
            var _clsGetOrderServicesMaintainEdit = new BllOrderServicesMaintain();
            var _dtGetOrderServicesMaintain = _clsGetOrderServicesMaintainEdit.GetSellSum(id, Users_Id, flag);
            if (_dtGetOrderServicesMaintain != null && _dtGetOrderServicesMaintain.Rows.Count > 0)
            {
                lbTotalReview.Text = _dtGetOrderServicesMaintain.Rows[0]["TotalMaterial"].ToString();
            }
        }
        private void GetOrderServicesMaintainDetailTemp()
        {
            var _clsGetOrderServicesMaintainEdit = new BllOrderServicesMaintain();
            var _dtGetOrderServicesMaintain = _clsGetOrderServicesMaintainEdit.GetOrderServicesMaintainDetailTemp(1, int.Parse(Session["Users_Id"].ToString()));
            if (_dtGetOrderServicesMaintain != null && _dtGetOrderServicesMaintain.Rows.Count > 0)
            {
                StoreOrderServicesMaintain_Detail.DataSource = _dtGetOrderServicesMaintain;
                StoreOrderServicesMaintain_Detail.DataBind();
            }
            else
            {
                grOrderServicesMaintain_Detail.GetStore().RemoveAll();
            }
        }
        private void GetOrderServicesMaintainFillterDate()
        {
            if (String.IsNullOrEmpty(txtDateBeginFillter.Text) && String.IsNullOrEmpty(txtDateEndFillter.Text))
            {
                var _clsGetOrderServicesMaintain = new BllOrderServicesMaintain();
                var _dtGetOrderServicesMaintain = _clsGetOrderServicesMaintain.GetOrderServicesMaintain(txtSearch.Text);
                if (_dtGetOrderServicesMaintain != null && _dtGetOrderServicesMaintain.Rows.Count > 0)
                {
                    StoreOrderServicesMaintain.DataSource = _dtGetOrderServicesMaintain;
                    StoreOrderServicesMaintain.DataBind();
                }
                else
                {
                    grOrderServicesMaintain.GetStore().RemoveAll();
                }
            }
            else
            {
                var _clsGetOrderServicesMaintain = new BllOrderServicesMaintain();
                var _dtGetOrderServicesMaintain =
                    _clsGetOrderServicesMaintain.GetOrderServicesMaintainFillter(txtDateBeginFillter.Text,
                        txtDateEndFillter.Text);
                if (_dtGetOrderServicesMaintain != null && _dtGetOrderServicesMaintain.Rows.Count > 0)
                {
                    StoreOrderServicesMaintain.DataSource = _dtGetOrderServicesMaintain;
                    StoreOrderServicesMaintain.DataBind();
                }
                else
                {
                    grOrderServicesMaintain.GetStore().RemoveAll();
                }
            }
        }
        private void GetAutoID()
        {
            var _clsBllCreateCode = new BllCreateCode();
            var _dtGetAutoID = _clsBllCreateCode.GetAutoID("Maintain_Code", "Order_Services_Maintain");
            if (_dtGetAutoID != null && _dtGetAutoID.Rows.Count > 0)
            {
                try
                {
                    txtOrderServicesMaintainCode.Text = "PBT-" + DateTime.Now.ToString("yyMM") + "-000" +
                                            Convert.ToString(int.Parse(_dtGetAutoID.Rows[0]["Auto_ID"].ToString()) +
                                                             int.Parse(1.ToString()));
                }
                catch (Exception ex)
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", ex.Message);
                }
            }
        }
        private bool CheckInsert()
        {
            bool check = false;
            var _clsCheck = new BllOrderServicesMaintain();
            var _dtCheck = _clsCheck.GetOrderServicesMaintain(txtSearch.Text);
            if (_dtCheck != null && _dtCheck.Rows.Count > 0)
            {
                foreach (DataRow dr in _dtCheck.Rows)
                {
                    if (txtOrderServicesMaintainCode.Text == dr["Maintain_Code"].ToString())
                    {
                        check = true;
                    }
                }
            }
            return check;
        }
        private bool CheckInsertDetail(int ID_Sell_Detail)
        {
            bool check = false;
            var _clsCheck = new BllOrderServicesMaintain();
            var _dtCheck = _clsCheck.GetOrderServicesMaintainDetailTemp(1, int.Parse(Session["Users_Id"].ToString()));
            if (_dtCheck != null && _dtCheck.Rows.Count > 0)
            {
                foreach (DataRow dr in _dtCheck.Rows)
                {
                    if (ID_Sell_Detail == int.Parse(dr["ID_Sell_Detail"].ToString()))
                    {
                        check = true;
                    }
                }
            }
            return check;
        }
        private void Insert()
        {
            if (_flag)
            {
                InsertValue();
            }
            else
            {
                if (CheckPermisstionDobclick())
                {
                    HTML.clsFormat.Alert_ERROR("Lỗi", "Bạn không được cấp phép thay đổi dữ liệu !");
                }
                else
                {
                    UpdateValue();
                }
            }
        }
        private void InsertValue()
        {
            try
            {
                if (CheckInsert())
                {
                    HTML.clsFormat.Alert_ERROR("Lỗi", "Chứng từ này đã tồn tại !");
                }
                else
                {
                    var _clsGetOrderServicesMaintainEdit = new BllOrderServicesMaintain();
                    var _dtGetOrderServicesMaintain = _clsGetOrderServicesMaintainEdit.GetOrderServicesMaintainDetailTemp(1,
                        int.Parse(Session["Users_Id"].ToString()));
                    if (_dtGetOrderServicesMaintain.Rows.Count < 0)
                    {
                        HTML.clsFormat.Alert_ERROR("Lỗi", "Vui lòng chọn vật tư để thêm mới !");
                    }
                    else
                    {
                        var obj = new EtOrderServicesMaintain
                        {
                            Maintain_Code = txtOrderServicesMaintainCode.Text,
                            Order_Sell_Id = String.IsNullOrEmpty(txtSell_Id.Text) ? (int?)null : int.Parse(txtSell_Id.Text),
                            Maintain_TaxCode = txtTaxCode.Text,
                            Maintain_DateBegin = DateTime.Parse(txtDateBegin.Text),
                            Users_Id = int.Parse(Session["Users_Id"].ToString()),
                            Note = txtDescription.Text,
                        };
                        BllOrderServicesMaintain.Insert(obj);
                        HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                        GetOrderServicesMaintain();
                        ClearText();
                    }
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void UpdateValue()
        {
            var obj = new EtOrderServicesMaintain
            {
                Maintain_ID = int.Parse(txtMaintain_ID.Text),
                Maintain_TaxCode = txtTaxCode.Text,
                Note = txtDescription.Text
            };
            BllOrderServicesMaintain.Update(obj);
            HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật thành công !");
            GetOrderServicesMaintain();
        }
        private void InsertDetail()
        {
            try
            {
                var sm = grOrderWithOrder.GetSelectionModel() as CheckboxSelectionModel;
                if (sm != null)
                    foreach (var row in sm.SelectedRows)
                    {
                        if (CheckInsertDetail(int.Parse(row.RecordID)))
                        {
                            HTML.clsFormat.Alert_ERROR("Lỗi", "Vật tư này đã tồn tại !");
                        }
                        else
                        {
                            var obj = new EtOrderServicesMaintainDetail
                            {
                                Maintain_ID = null,
                                Order_Sell_Detail_Id = int.Parse(row.RecordID),
                                Users_Id = int.Parse(Session["Users_Id"].ToString()),
                                Flag = 1
                            };
                            BllOrderServicesMaintain.InsertDetail(obj);
                            HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới vật tư thành công");
                        }
                    }
                GetOrderServicesMaintainDetailTemp();
                GetOrderServicesMaintainSum(null, 1, int.Parse(Session["Users_Id"].ToString()));
                WindowOrderWithOrder.Close();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void UdpateDetail()
        {
            try
            {
                var sm = grOrderWithOrder.GetSelectionModel() as CheckboxSelectionModel;
                if (sm != null)
                    foreach (var row in sm.SelectedRows)
                    {
                        if (CheckInsertDetail(int.Parse(row.RecordID)))
                        {
                            HTML.clsFormat.Alert_ERROR("Lỗi", "Vật tư này đã tồn tại !");
                        }
                        else
                        {
                            var obj = new EtOrderServicesMaintainDetail
                            {
                                Maintain_ID = int.Parse(txtMaintain_ID.Text),
                                Order_Sell_Detail_Id = int.Parse(row.RecordID),
                                Users_Id = int.Parse(Session["Users_Id"].ToString()),
                                Flag = 0
                            };
                            BllOrderServicesMaintain.InsertDetail(obj);
                            HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới vật tư thành công");
                        }
                    }
                GetOrderServicesMaintainEdit(int.Parse(txtMaintain_ID.Text));
                GetOrderServicesMaintainSum(int.Parse(txtMaintain_ID.Text), 0, int.Parse(Session["Users_Id"].ToString()));
                WindowOrderWithOrder.Close();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }
        private void UpdateDate(int Maintain_Detail_ID, DateTime Maintain_DateEnd)
        {
            try
            {
                if (_flag)
                {
                    var obj = new EtOrderServicesMaintainDetail
                    {
                        Maintain_Detail_ID = Maintain_Detail_ID,
                        Maintain_DateEnd = Maintain_DateEnd,
                    };
                    BllOrderServicesMaintain.UpdateDetail(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật thành công !");
                    GetOrderServicesMaintainDetailTemp();
                    GetOrderServicesMaintainSum(null, 1, int.Parse(Session["Users_Id"].ToString()));
                }
                else
                {
                    var obj = new EtOrderServicesMaintainDetail
                    {
                        Maintain_Detail_ID = Maintain_Detail_ID,
                        Maintain_DateEnd = Maintain_DateEnd,
                    };
                    BllOrderServicesMaintain.UpdateDetail(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật thành công !");
                    GetOrderServicesMaintainEdit(int.Parse(txtMaintain_ID.Text));
                    GetOrderServicesMaintainSum(int.Parse(txtMaintain_ID.Text), 0, null);
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }
        private void DeleteDetail(int OrderServicesMaintain_Detail_Id, int Flag)
        {
            try
            {
                var obj = new EtOrderServicesMaintainDetail { Maintain_Detail_ID = OrderServicesMaintain_Detail_Id, Flag = Flag };
                BllOrderServicesMaintain.DeleteDetail(obj);
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void DeleteAdd(int Users_Id, int Flag)
        {
            try
            {
                var obj = new EtOrderServicesMaintainDetail { Users_Id = Users_Id, Flag = Flag };
                BllOrderServicesMaintain.DeleteAdd(obj);
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void Review()
        {
            try
            {
                var obj = new EtOrderServicesMaintain
                {
                    Maintain_ID = int.Parse(txtReviewMaintain_ID.Text),
                    Review = true,
                    Uses_Review = int.Parse(Session["Users_Id"].ToString())
                };
                BllOrderServicesMaintain.Review(obj);
                HTML.clsFormat.Alert_INFO("Thông Báo", "Xét duyệt thành công !");
                GetOrderServicesMaintainReview(int.Parse(txtReviewMaintain_ID.Text));
                GetOrderServicesMaintainSumReview(int.Parse(txtReviewMaintain_ID.Text), 0, null);
                GetOrderServicesMaintain();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void UnReview()
        {
            try
            {
                var obj = new EtOrderServicesMaintain
                {
                    Maintain_ID = int.Parse(txtReviewMaintain_ID.Text),
                    Review = false,
                    Uses_Review = int.Parse(Session["Users_Id"].ToString())
                };
                BllOrderServicesMaintain.Review(obj);
                HTML.clsFormat.Alert_INFO("Thông Báo", "Huỷ xét duyệt thành công !");
                GetOrderServicesMaintainReview(int.Parse(txtReviewMaintain_ID.Text));
                GetOrderServicesMaintainSumReview(int.Parse(txtReviewMaintain_ID.Text), 0, null);
                GetOrderServicesMaintain();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        #endregion

        #region[ClearText]
        private void ClearText()
        {
            FormPanelOrderServicesMaintain.Reset();
            DeleteAdd(int.Parse(Session["Users_Id"].ToString()), 1);
            grOrderServicesMaintain_Detail.GetStore().RemoveAll();
            lbTotalOver.Text = "0";
        }
        #endregion

        #region[Permisstion]
        private void CheckPermisstion()
        {
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.ServicesMaintain, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnAdd == false)
                {
                    btnAddNew.Visible = false;
                }
                if (Is_BtnEdit == false)
                {
                    btnEdit.Visible = false;
                }
                if (Is_BtnView == false)
                {
                    PanelOrderServicesMaintain.Visible = false;
                }
                if (Is_BtnDel == false)
                {
                    btnDel.Visible = false;
                }
                if (Is_BtnPrint == false)
                {
                    btnExport.Visible = false;
                }
                if (Is_BtnReview == false)
                {
                    btnReview.Visible = false;
                }
                if (Is_BtnCancel == false)
                {
                    btnUnReview.Visible = false;
                }
            }
        }
        private bool CheckPermisstionDobclick()
        {
            bool check = false;
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.ServicesMaintain, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnEdit == false)
                {
                    check = true;
                }
            }
            return check;
        }
        #endregion
    }
}